<?php
/**
 * NOTICE OF LICENSE
 *
 * This source file is subject to the  Academic Free License (AFL 3.0)
 * that is available through the world-wide-web at this URL:
 * http://opensource.org/licenses/afl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to support@zprofess.com so we can send you a copy immediately.
 *
 * @category   	Prestashop module
 * @package		Help Desk
 * @version 	1.0
 * @author 		Kurosaki <kurosaki@zprofess.com>
 * @copyright   Copyright (c) 2012 ZPROFESS. (http://zprofess.com)
 * @license    	http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 */
include_once(dirname(__FILE__)."/classes/ZpriorityObject.php");
require_once(_PS_MODULE_DIR_.'zhelpdesk/lib/zprofess/ZhelpdeskadminTab.php');
class ZadminPriority extends ZhelpdeskadminTab
{

	public function __construct()
	{
		global $cookie;
		
		$this->table = 'zhelpdesk_ticket_priority';
	 	$this->className = 'ZpriorityObject';
	 	$this->lang = true;
	 	$this->edit = true;
	 	$this->delete = true;

		$this->fieldsDisplay = array(
		'id_zhelpdesk_ticket_priority' => array('title' => $this->l('ID'), 'align' => 'center', 'width' => 30),
		'priority' => array('title' => $this->l('Name'), 'width' => 100),
		'description' => array('title' => $this->l('Description'), 'width' => 500, 'maxlength' => 90, 'orderby' => false),
		'position' => array('title' => $this->l('Position'), 'width' => 40,'filter_key' => 'position', 'align' => 'center', 'position' => 'position'),
		'active' => array('title' => $this->l('Displayed'), 'active' => 'priority', 'align' => 'center', 'type' => 'bool', 'orderby' => false));
		
		$this->identifiersDnd = array('id_zhelpdesk_ticket_priority'=> 'id_zhelpdesk_ticket_priority');
		$this->identifier = 'id_zhelpdesk_ticket_priority';
		$this->_select = 'position';
		$this->_defaultOrderBy = 'position';
		
		parent::__construct();
		
	}

	/**
	 * Load js và css
		bf-status.js => change text when change color background and color text
		jquery-colorpicker.js => create html code color for color background and text
	 */
	function loadJS_CSS()
	{
		echo '
			<script type="text/javascript" src="'.__PS_BASE_URI__.'js/jquery/jquery-colorpicker.js"></script>
			<script type="text/javascript" src="'.__PS_BASE_URI__.'modules/zhelpdesk/js/backoffice/backoffice-status.js"></script>
			<style type="text/css">
				.text-view {
				    font-size: 1.2em;
				    height: 18px;
				    padding-top: 3px;
				    text-align: center;
				    width: 138px;
				}
			</style>
			
			';
	}
	/**
	*
	*
	*/
	public function displayForm($token = NULL)
	{
		global $currentIndex, $cookie;
		parent::displayForm();
		
		if (!($obj = $this->loadObject(true)))
			return;
		$this->loadJS_CSS();
		$langtags = 'priority¤description';
		echo '<form action="'.$currentIndex.'&submitAdd'.$this->table.'=1&token='.$this->token.'" method="post" enctype="multipart/form-data">
			'.($obj->id ? '<input type="hidden" name="id_'.$this->table.'" value="'.$obj->id.'" /> ' : '');
		echo '
			<fieldset style="width:900px;">
				<legend>'.$this->l('priority').'</legend>';
				
				echo $this->addElement('text',array (
						'label' => 'Name',
						'name' => 'priority',
						'langtags' => $langtags,
						'width' => 200,
						'note' => '',
						'more' => ''
				),true);
				echo $this->addElement('area',array (
						'label' => 'Description',
						'name' => 'description',
						'langtags' => $langtags,
						'width' => 200,
						'note' => '',
						'more' => ''
				),true);
				
				echo '
				<br/>
				<label>Backgroud Color</label>
				<div class="margin-form">
						<input width="20px" type="color" data-hex="true" onchange="changbackground();" class="color mColorPickerInput" name="backgroud_color" value="'.($obj->backgroud_color).'" />
					</div>';
				echo '
				<label>Text Color</label>
				<div class="margin-form">
						<input width="20px" type="color" data-hex="true" onchange="changtext();" class="color mColorPickerInput" name="text_color" value="'.($obj->text_color).'" />
					</div>';
				
				echo $this->addElement('yesno',array (
						'label' => 'Active',
						'name' => 'active',
						'width' => 200,
						'value' => $obj->active,
						'note' => '',
						'more' => ''
				),true);
				
				
		echo '
		<div class="margin-form">
				<div id="text-view" class="text-view" style="color:'.$obj->text_color.'; background-color:'.$obj->backgroud_color.'">
					'.($obj->id?htmlentities(stripslashes($this->getFieldValue($obj, 'priority', 1)), ENT_COMPAT, 'UTF-8'):"Priority").'
				</div>
			</div>';
		echo '
			<div class="margin-form">
					<input type="submit" value="'.$this->l('Save').'" name="submitAdd'.$this->table.'AndBackToParent" class="button" />
				</div>
				<div class="small"><sup>*</sup> '.$this->l('Required field').'</div>
			</fieldset>
		</form>';
	}
	/**
	 * Display same dislay parent but add style for item
	 ZpriorityObject::getStyleListContent => create style for column name is color text and background confix 
	 jquery.tablednd_0_5.js => add js for table when change position
	 bf-pnd.js => call ajax change position and save database
	 */	
	public function display($token=NULL) {
		parent::display();
		echo ZpriorityObject::getStyleListContent ($this->_list);
		// add js when change position
		echo ' 
			<script type="text/javascript" src="../js/jquery/jquery.tablednd_0_5.js"></script>
			<script type="text/javascript">
				var token = \''.($token!=NULL ? $token : $this->token).'\';
				var come_from = \''.$this->table.'\';
				var alternate = \''.($this->_orderWay == 'DESC' ? '1' : '0' ).'\';
			</script>
			<script type="text/javascript" src="../modules/zhelpdesk/js/backoffice/backoffice-pnd.js"></script>
			';
	}
}
